package com.hanthink.web.controller.pc;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.text.Convert;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.system.domain.pc.EmJobItem;
import com.hanthink.system.service.impl.EmClosemachineLogServiceImpl;
//import com.sun.corba.se.impl.oa.toa.TOA;
//import com.sun.corba.se.impl.oa.toa.TOAFactory;
//import io.swagger.models.auth.In;
import org.aspectj.lang.annotation.Before;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hanthink.common.annotation.Log;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.system.domain.manhour.ProductionPlanInShift;
import com.hanthink.system.service.manhour.IProductionPlanInShiftService;
import com.hanthink.common.utils.poi.ExcelUtil;
import com.hanthink.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;


/**
 * 用工计划数据填报Controller
 * 
 * @author hanthink
 * @date 2021-12-06
 */
@RestController
@RequestMapping("/system/ProductionPlanInShift")
public class ProductionPlanInShiftController extends BaseController
{
    @Autowired
    private IProductionPlanInShiftService productionPlanInShiftService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询用工计划数据填报列表
     */
    @PreAuthorize("@ss.hasPermi('system:ProductionPlanInShift:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductionPlanInShift productionPlanInShift)
    {
        startPage();
        List<ProductionPlanInShift> list = productionPlanInShiftService.selectProductionPlanInShiftList(productionPlanInShift);
        return getDataTable(list);
    }


    @Log(title = "用工计划数据填报", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('equipment:jobItem:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ProductionPlanInShift> util = new ExcelUtil<ProductionPlanInShift>(ProductionPlanInShift.class);
        List<ProductionPlanInShift> itemList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = productionPlanInShiftService.importPlanitem(itemList, updateSupport, operName);
        return AjaxResult.success(message);
    }


    @GetMapping("/importTemplate") // 下载导入模板 fmc
    public AjaxResult importTemplate() {
        ExcelUtil<ProductionPlanInShift> util = new ExcelUtil<ProductionPlanInShift>(ProductionPlanInShift.class);
        return util.importTemplateExcel("用工计划数据填报");
    }

    /**
     * 导出用工计划数据填报列表
     */
    @PreAuthorize("@ss.hasPermi('system:ProductionPlanInShift:export')")
    @Log(title = "用工计划数据填报", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProductionPlanInShift productionPlanInShift)
    {
        List<ProductionPlanInShift> list = productionPlanInShiftService.selectProductionPlanInShiftList(productionPlanInShift);
        ExcelUtil<ProductionPlanInShift> util = new ExcelUtil<ProductionPlanInShift>(ProductionPlanInShift.class);
        return util.exportExcel(list, "ProductionPlanInShift");
    }

    /**
     * 获取用工计划数据填报详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ProductionPlanInShift:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(productionPlanInShiftService.selectProductionPlanInShiftById(id));
    }

    /**
     * 新增用工计划数据填报
     */
    @PreAuthorize("@ss.hasPermi('system:ProductionPlanInShift:add')")
    @Log(title = "用工计划数据填报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductionPlanInShift productionPlanInShift)
    {
        return toAjax(productionPlanInShiftService.insertProductionPlanInShift(productionPlanInShift));
    }

    /**
     * 修改用工计划数据填报
     */
    @PreAuthorize("@ss.hasPermi('system:ProductionPlanInShift:edit')")
    @Log(title = "用工计划数据填报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductionPlanInShift productionPlanInShift)
    {
        return toAjax(productionPlanInShiftService.updateProductionPlanInShift(productionPlanInShift));
    }

    /**
     * 删除用工计划数据填报
     */
    @PreAuthorize("@ss.hasPermi('system:ProductionPlanInShift:remove')")
    @Log(title = "用工计划数据填报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        ArrayList<Long> idsnew =new ArrayList<Long>();
        //限制不能删除历史生产计划
        for (int i = 0; i <ids.length ; i++) {
            ProductionPlanInShift pp= productionPlanInShiftService.selectProductionPlanInShiftById(ids[i]);
            java.util.Date day=new Date();
            String sdate = "";
            String s = "yyyy-MM-dd";
            SimpleDateFormat f = new SimpleDateFormat(s);
            //生成计划开班时间
            switch (pp.getPshiftid()){
                case "白班":
                case "转班(白)":
                    sdate = f.format(pp.getPlanworkdatetime()) + " 08:30:00";
                    break;
                case "夜班":
                    sdate = f.format(pp.getPlanworkdatetime()) + " 20:30:00";
                    break;
                case "转班(夜)":
                    sdate = f.format(pp.getPlanworkdatetime()) + " 16:30:00";
                    break;
                case "中班":
                    sdate = f.format(pp.getPlanworkdatetime())  +" 00:30:00";
                    break;
            }
            if (strToDate(sdate).after(day)){
                idsnew.add(ids[i]);
            }
        }
        Long[] ida = new Long[idsnew.size()];
        idsnew.toArray(ida);
        if (idsnew.size()>0){
            return toAjax(productionPlanInShiftService.deleteProductionPlanInShiftByIds(ida));
        }
        else{
            return toAjax(1);
        }
    }
    public static Date strToDate(String  time) {
        Date newTime=new Date();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  // 字符串转换成时间
        try {
            newTime = sd.parse(time);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return  newTime;
    }
}
